package com.niit.bc.biz.impl;

import com.niit.bc.bean.UserBean;
import com.niit.bc.biz.UserBiz;
import com.niit.bc.dao.UserDao;
import com.niit.bc.dao.impl.UserDaoImpl;

import javax.sql.RowSet;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserBizImpl implements UserBiz {
    UserDao userDao = new UserDaoImpl();

    //调用数据库访问层，验证是否有该用户，并同时更新登录次数，并返回该用户编号
    public UserBean findUser(UserBean userBean) {
        RowSet rs = userDao.findUser(userBean);
        try {
            if (rs != null && rs.next()) {
                int row = userDao.updateUserLoginTime(rs.getInt(7), rs.getInt(1));
                if (row > 0) {
                    int i = 1;
                    userBean.setUserId(rs.getInt(i++));
                    userBean.setUserName(rs.getString(i++));
                    userBean.setUserPwd(rs.getString(i++));
                    userBean.setUserEmail(rs.getString(i++));
                    userBean.setUserMobile(rs.getString(i++));
                    userBean.setUserQq(rs.getString(i++));
                    userBean.setUserLoginTime(rs.getInt(i++) + 1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userBean;
    }

    public UserBean findUserByUserName(String userName) {
        // TODO Auto-generated method stub
        UserBean user = null;
        ResultSet rs = userDao.findUserByUserName(userName);
        try {
            if (rs != null && rs.next()) {
                user = new UserBean();
                user.setUserId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setUserPwd(rs.getString(3));
                user.setUserEmail(rs.getString(4));
                user.setUserMobile(rs.getString(5));
                user.setUserQq(rs.getString(6));
                user.setUserLoginTime(rs.getInt(7));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return user;
    }

}
